﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text.RegularExpressions;

using MySql.Data.MySqlClient;

namespace ukeasbjWeb
{
    public partial class WasteBookEdit : Bonus ///System.Web.UI.Page
    {
        
        //合同主键
        string sWasteBook_No;

        public enum Func
        {
            InputWasteBook,
            NewOne,
        }

        //		public Func eFunctions;

        /// <summary>
        /// 读取功能标识
        /// </summary>
        private Func Get_Function(string strFunction)
        {
            Regex NotEditFirstPayment = new Regex("([a-zA-Z]{1,}):([0-9]{1,})");
            Regex NewOneFirstPayment = new Regex("(NewOne)");
            //			Regex EditFirstPayment = new Regex("([0-9]{1,})");
            string[] temp;

            if (NotEditFirstPayment.IsMatch(strFunction))
            {
                temp = NotEditFirstPayment.Split(strFunction);
                if (temp[1] == "InputWasteBook")
                {// 只读方式读取合同数据
                    return Func.InputWasteBook;
                }
            }

            if (NewOneFirstPayment.IsMatch(strFunction))
            {
                return Func.NewOne;
            }

            return Func.InputWasteBook;
        }
 
       
        protected void Page_Load(object sender, EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            Common_SXD.Is_LongIn(Response, Session, Request);

            bool bIs_Cashier = Convert.ToBoolean(Session["Is_Cashier"]);
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);

            if (!bIs_Cashier && !bIs_Admin  )
            {
                Response.Write("<h2 align='center'><font face='宋体' color='red'>");
                Response.Write("<br>对不起，您不能使用本功能。" +
                    "<br>请用其它身份登录。");
                Response.Write("</fong></h2>");
                Response.End();

            }
            ///////////// 读出合同主键
            sWasteBook_No = Request["WasteBook_No"];
            Session["Flag"] = sWasteBook_No;
            ///////////// 分离加在合同主键前部的控制字符
            Regex NotEditFirstPayment = new Regex("([a-zA-Z]{1,}):([a-zA-Z]{0,1}[0-9]{1,})");
            Regex NewOneFirstPayment = new Regex("(NewOne)");
            string[] temp;

            if (NotEditFirstPayment.IsMatch(sWasteBook_No))
            {
                temp = NotEditFirstPayment.Split(sWasteBook_No);
                if (temp[1] == "InputWasteBook")
                {// 
                    sWasteBook_No = temp[2];
                    if (!IsPostBack)
                        Init_InputWasteBook();

                    Label_Msg.Text = sWasteBook_No;
                    return;
                }


            }

            if (!IsPostBack)
            {
                if (NewOneFirstPayment.IsMatch(sWasteBook_No))
                {
                    Label_Msg.Text = sWasteBook_No;
                    Init_NewOne(); //初始化：添加新流水账数据
                    return;
                }
                /*
                SqlConnection sqlCnn = new SqlConnection(strCnn);
                // 现金_银行，绑定到 DDList_Cash_Counters 中
                string sSql = "select * from Cash_Counters order by Code_Cash_Counters asc";
                SqlDataAdapter sqlDA = new SqlDataAdapter(sSql, sqlCnn);
                sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                DataSet bsDataSet = new DataSet();
                sqlDA.Fill(bsDataSet, "Cash_Counters");
                DDList_Cash_Counters.DataSource = bsDataSet.Tables["Cash_Counters"];
                DDList_Cash_Counters.DataBind();
                sqlCnn.Close();
                */ 
                string sSql = "select * from Cash_Counters order by Code_Cash_Counters asc";
                Common_SXD.Init_DropDownList(DDList_Cash_Counters, "Cash_Counters", sSql);

                ///sSql_Add_DropDownList(DDList_Cash_Counters, "Cash_Counters", sSql);

            }

        }

        /// <summary>
        /// 初始化:新的流水账记录界面
        /// </summary>
        private void Init_NewOne()
        {
            Response.Write("<h2 align='center'><font face='宋体' color='red'>");
            Response.Write("<br>对不起，添加新数据功能，不能使用。" );
            Response.Write("</fong></h2>");
            Response.End();

                
        }

        /// <summary>
        /// 初始化:流水账数据输入修改界面
        /// </summary>
        private void Init_InputWasteBook()
        {
            bool bIs_Admin = Convert.ToBoolean(Session["Is_Admin"]);
            bool bIs_O_M = Convert.ToBoolean(Session["Is_O_M"]);
            bool bIs_Collector = Convert.ToBoolean(Session["Is_Collector"]);
            bool bIs_Accountant = Convert.ToBoolean(Session["Is_Accountant"]);
            bool bIs_Receptionist = Convert.ToBoolean(Session["Is_Receptionist"]);
            bool bIs_Writer = Convert.ToBoolean(Session["Is_Writer"]);
            bool bIs_Counsellor = Convert.ToBoolean(Session["Is_Counsellor"]);

            Response.Write("<li>修改：流水账数据。</li>");

            /*
            SqlConnection sqlCnn = new SqlConnection(strCnn);
            // 根据传入的合同编号，生成查询 SQL 语句
            string sSql = "select top 500 * from WasteBook where WasteBook_No = " + "'" + sWasteBook_No + "'";
            SqlDataAdapter sqlDA = new SqlDataAdapter(sSql, sqlCnn);
            sqlDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            DataSet bsDataSet = new DataSet();
            sqlDA.Fill(bsDataSet, "WasteBook");
            */
            InitControls_InputWasteBook();

            string sSql = "select  * from WasteBook where WasteBook_No like '" + sWasteBook_No + "'";
            DataSet bsDataSet = Init_DataSet("WasteBook", sSql);

            if (bsDataSet.Tables["WasteBook"].Rows.Count == 1)
            {
                DataRow rowUser = bsDataSet.Tables["WasteBook"].Rows[0]; // 只有一行
                // 显示用户信息
                /*
                    [Is_Active] [bit]  NOT NULL,		
                */
                TextBox_WasteBook_No.Text = rowUser["WasteBook_No"].ToString().Trim();
                TextBox_Series_No.Text = rowUser["Series_No"].ToString().Trim();

                TextBox_Brief.Text = rowUser["Brief"].ToString().Trim();
                TextBox_Date_Occur.Text = Common_SXD.TimeToYMD(rowUser["Date_Occur"].ToString().Trim());
                TextBox_Credit_Income.Text = rowUser["Credit_Income"].ToString().Trim();
                TextBox_Debit_Payout.Text = rowUser["Debit_Payout"].ToString().Trim();
                TextBox_Balance.Text = rowUser["Balance"].ToString().Trim();

                TextBox_Type_Report.Text = rowUser["Type_Report"].ToString().Trim();
                TextBox_Notes.Text = rowUser["Notes"].ToString().Trim();

                Common_SXD.SetSelectedIndex(rowUser["Cash_Counters"].ToString().Trim(), DDList_Cash_Counters);

                if ((rowUser["Is_New"].ToString().Trim() == "True") ||
                    (rowUser["Is_New"].ToString().Trim() == "1"))
                    CheckBox_Is_New.Checked = true;
                else
                    CheckBox_Is_New.Checked = false;

            }
            ///sqlCnn.Close();

        }

        /// <summary>
        /// 插入：新的流水账记录到数据库
        /// </summary>
        private void Insert_NewOne()
        {

        }

        /// <summary>
        /// 更新修改：流水账记录
        /// </summary>
        private void Update_InputWasteBook()
        {
            //客户信息写入数据库
            string sSql = "Update WasteBook Set";


            sSql = sSql_Add_TextBox(TextBox_Brief, "Brief", sSql);
            TextBox_Balance.Enabled = true;
            sSql = sSql_Add_Number(TextBox_Balance, "Balance", sSql);
            TextBox_Balance.Enabled = false;

            sSql += " Notes = '" + TextBox_Notes.Text.Trim() + "' ";
            sSql += " where WasteBook_No like '" + Label_Msg.Text.Trim() + "' ";

            Common_SXD.MySql_ExecuteNonQuery(sSql, "", "打开数据库出错！", Response);

        }

        private void InitControls_InputWasteBook()
        {

            false_Enable_All_Controls();

            TextBox_Notes.Enabled = true;
            TextBox_Brief.Enabled = true;
            string sSql = "select * from Cash_Counters order by Code_Cash_Counters asc";
            Common_SXD.Init_DropDownList(DDList_Cash_Counters, "Cash_Counters", sSql);
 
        }


        /// <summary>
        ///  关闭所有的页面交互控制。
        /// </summary>
        private void false_Enable_All_Controls()
        {
            TextBox_Date_Occur.Enabled = false;
            TextBox_Credit_Income.Enabled = false;
            TextBox_Debit_Payout.Enabled = false;
            TextBox_Balance.Enabled = false;
            TextBox_Type_Report.Enabled = false;
            DDList_Cash_Counters.Enabled = false;
            CheckBox_Is_New.Enabled = false;
            TextBox_WasteBook_No.Enabled = false;
            TextBox_Brief.Enabled = false;
            TextBox_Notes.Enabled = false;
            TextBox_Balance.Enabled = false;
            TextBox_Series_No.Enabled = false;

            
        }  


        protected void Bt_OK_Click(object sender, EventArgs e)
        {
            //	eFunctions = Get_Function(Session["Flag"].ToString().Trim());
            switch (Get_Function(Session["Flag"].ToString().Trim()))
            {
                case Func.NewOne: 	// 添加一个新的记录
                    Insert_NewOne();
                    break;

                case Func.InputWasteBook:		// 修改流水账数据
                    Update_InputWasteBook();
                    break;
                default:
                    break;

            }
        }

        protected void Bt_Make_Balance_Fast_Click(object sender, EventArgs e)
        {
            string sSql = "select  sum(credit_Income) - sum(Debit_Payout) from WasteBook  where ";
            
            sSql += " WasteBook_No <= '" + TextBox_WasteBook_No.Text.ToString().Trim() + "' and ";
            sSql += " Cash_Counters = '" + DDList_Cash_Counters.SelectedValue.ToString().Trim() + "' ";
            
            MySqlConnection MySqlconn = new MySqlConnection();
            MySqlDataReader MysqlReader = Common_SXD.Open_MySqlDataReader(sSql, MySqlconn, "打开数据库出错：", Response);

            if (MysqlReader.Read())
            {
                TextBox_Balance.Text = MysqlReader.GetString(0);
            }

            MysqlReader.Close(); // 关闭sqDataReader
            MySqlconn.Close(); // 关闭数据库连接

        }

        

        

    }
}
